package cn.xn.hrms.domain.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.PersonalDTO;
import cn.xn.hrms.api.dto.req.PersonalReqDTO;
import cn.xn.hrms.infrastructure.dataobject.PersonalDO;
import cn.xn.hrms.domain.enums.ResultEnum;
import cn.xn.hrms.domain.exception.BaseException;
import cn.xn.hrms.domain.service.DepartmentService;
import cn.xn.hrms.domain.service.PersonalService;
import cn.xn.hrms.domain.service.PositionService;
import cn.xn.hrms.infrastructure.dao.PersonalDao;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ysz
 */
@Service
@Slf4j
public class PersonalServiceImpl implements PersonalService {

    @Resource
    PersonalDao personalDao;
    @Resource
    DepartmentService departmentService;
    @Resource
    PositionService positionService;

    @Override
    public PageInfo<PersonalDO> findAll(int pageNum, int pageSize, int departmentId, String personalName, int workStatus) {
        PageHelper.startPage(pageNum, pageSize);
        if (ObjectUtils.isEmpty(personalName)) {
            personalName = null;
        }
        List<PersonalDO> personals = personalDao.selectAll(departmentId, personalName, workStatus);
        return new PageInfo<>(personals);
    }

    @Override
    public List<PersonalDO> All() {
        return personalDao.selectAll(0, null, 0);
    }

    @Override
    public PersonalDO find(int id) {
        PersonalDO personalDO = personalDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(personalDO)) {
            throw new BaseException(ResultEnum.PERSONAL_NOT_EXIST);
        }
        return personalDO;
    }

    @Override
    public void insert(PersonalReqDTO personalReqDTO) {

        log.info("新增员工，入参：{}", JSON.toJSONString(personalReqDTO));

        if (!ObjectUtils.isEmpty(personalReqDTO.getDepartmentId())) {
            departmentService.find(personalReqDTO.getDepartmentId());
        }
        if (!ObjectUtils.isEmpty(personalReqDTO.getPositionId())) {
            positionService.find(personalReqDTO.getPositionId());
        }
        PersonalDTO personalDTO = new PersonalDTO();
        BeanUtils.copyProperties(personalReqDTO, personalDTO);
        personalDao.insertSelective(personalDTO);
    }

    @Override
    public void deleteByIdIn(Collection<Integer> idList) {
        personalDao.updateIsDeletedByIdIn(1, idList);
    }

    @Override
    public void updateById(Integer id, PersonalReqDTO personalReqDTO) {
        PersonalDO personalDO = personalDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(personalDO)) {
            throw new BaseException(ResultEnum.POSITION_NOT_EXIST);
        }
        PersonalDTO personalDTO = new PersonalDTO();
        BeanUtils.copyProperties(personalDO, personalDTO);
        BeanUtils.copyProperties(personalReqDTO, personalDTO);
        personalDTO.setPersonalId(id);
        personalDao.updateByPrimaryKeySelective(personalDTO);
    }
}
